package edu.uob;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class TableManager {

    private static final String DEFAULT_DB_PATH = "." + File.separator + "databases" + File.separator + CmdType.getDbName();

    public static String dropTable(String tableName) {
        String pathStr = DEFAULT_DB_PATH + File.separator + tableName + ".tab";
        System.out.println("db=" + CmdType.getDbName());
        System.out.println("tbl=" + tableName);

        Path path = Paths.get(pathStr);
        try {
            if (Files.exists(path) && Files.isRegularFile(path)) {
                if (Files.deleteIfExists(path)) {
                    return "[OK]";
                } else {
                    return "[ERROR]: Unable to drop table '" + tableName + "'.";
                }
            } else {
                return "[ERROR]: Table '" + tableName + "' does not exist.";
            }
        } catch (IOException e) {
            return "[ERROR]: An error occurred while dropping table '" + tableName + "'.";
        }
    }
}
